package com.app.controller.backstage;

import com.app.model.AdminUserModel;
import com.app.model.CategoryModel;
import com.app.model.MapModel;
import com.app.service.AdminMapService;
import com.app.service.AdminUserService;
import com.app.service.CategoryService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.app.vo.CategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RestController
@RequestMapping("/admin")
public class AdminMapController {
    @Autowired
    private AdminMapService adminMapService;
    @Autowired
    private AdminUserService adminUserService;
    @Autowired
    private CategoryService categoryService;
    //下拉框列表
    @GetMapping("/getDropList")//下拉框查询
    public Result<Object> getDropList(
            HttpServletRequest request) {
//        String token = request.getHeader("authori-zation");
//        if (token == null) {
//            return Result.error(405, "token不存在");
//        }
//        String userId = TokenUtils.verify(token);
//        AdminUserModel user = adminUserService.getUserByUserId(Integer.valueOf(userId));
//        if (user == null) {
//            return Result.error(406, "用户不存在");
//        }
        CategoryVo vo = new CategoryVo();
        List<CategoryModel> phoneListByPid = categoryService.getCategoryByPid(23);
        return Result.success(phoneListByPid);
    }
    //新增和编辑
    @PostMapping("/insertAndUpdateMap")
    public Result<Object> insertAndUpdateMap(@RequestBody MapModel mapModel) {
        if (mapModel.getId() > 0) {
            mapModel.setUpdateTime(CommonHelp.getNowTimestamp());
            adminMapService.update(mapModel);
            return Result.success("操作成功");
        } else {
            mapModel.setCreateTime(CommonHelp.getNowTimestamp());
            adminMapService.insert(mapModel);
            return Result.success("操作成功");
        }
    }

    //删除
    @GetMapping("/deleteMap")
    public Result<Object> deleteMap(Integer id) {
        adminMapService.deleteById(id);
        return Result.success("操作成功");
    }

    //列表
    @GetMapping("/getMapList")
    public Result<Object> getMapList(Integer page, Integer limit, String keyword, Integer type, Integer status, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        return Result.success(adminMapService.selectList(page, limit, keyword, type, status));
    }
    //查看详情
    @GetMapping("/getMapInfo")
    public Result<Object> getMapInfo(Integer id, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        return Result.success(adminMapService.selectById(id));
    }
}
